/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bittorentlike;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import define.Define;
import java.nio.ByteBuffer;

/**
 *
 * @author Xuan Cau
 */
public class Send
{
    public Send(){}
    public void SendHello()
    {
        int len = 1;
        ByteBuffer buf = ByteBuffer.allocate(4 + len);
        buf.putInt(len);
        buf.put((byte)Define.HELLO);
        byte[] data = buf.array();
        SendRequest(data);
    }
    public void SendRespondHello(byte[] data)
    {
        int len = 1 + data.length;
        ByteBuffer buf = ByteBuffer.allocate(4 + len);
        buf.putInt(len);
        buf.put((byte)Define.RESPOND_HELLO);
        buf.put(data);
        byte[] data_send = buf.array();
        SendRequest(data_send);
    }
    public void SendCofig(int port)
    {
        int len = 4;
        ByteBuffer buf = ByteBuffer.allocate(4 + len);
        buf.putInt(len);
        buf.put((byte)Define.CONFIG);
        buf.putInt(port);
        byte[] data = buf.array();
        SendRequest(data);
    }
    public void SendRespondConfig()
    {
        
    }
    public void SendSearch()
    {
        int len = 1;
        ByteBuffer buf = ByteBuffer.allocate(4 + len);
        buf.putInt(len);
        buf.put((byte)Define.SEARCH);
        byte[] data = buf.array();
        SendRequest(data);
    }
    public void SendRespondSearch(byte[] data)
    {
        int len = 1 + data.length;
        ByteBuffer buf = ByteBuffer.allocate(4 + len);
        buf.putInt(len);
        buf.put((byte)Define.RESPOND_SEARCH);
        buf.put(data);
        byte[] data_send = buf.array();
        SendRequest(data_send);
    }
    public void SendDownload(byte[] chunk)
    {
        int len = 1 + chunk.length + 2;
        ByteBuffer buf = ByteBuffer.allocate(4 + len);
        buf.putInt(len);
        buf.put((byte)Define.DOWNLOAD);
        buf.putShort((short)chunk.length);
        buf.put(chunk);
        byte[] data = buf.array();
        SendRequest(data);
    }
    public void SendRespondDownload(byte[] data)
    {
        int len = 1 + data.length;
        ByteBuffer buf = ByteBuffer.allocate(4 + len);
        buf.putInt(len);
        buf.put((byte)Define.RESPOND_DOWNLOAD);
        buf.put(data);
        byte[] data_send = buf.array();
        SendRequest(data_send);
    }
    public void SendDownloadTorrent(byte[] data)
    {
        int len = 1 + data.length;
        ByteBuffer buf = ByteBuffer.allocate(4 + len);
        buf.putInt(len);
        buf.put((byte)Define.DOWNLOAD_TORRENT);
        buf.put(data);
        byte[] data_send = buf.array();
        SendRequest(data_send);
    }
    public void SendRespondDownloadTorrent(byte[] data)
    {
        int len = 1 + data.length;
        ByteBuffer buf = ByteBuffer.allocate(4 + len);
        buf.putInt(len);
        buf.put((byte)Define.RESPOND_DOWNLOAD_TORRENT);
        buf.put(data);
        byte[] data_send = buf.array();
        SendRequest(data_send);
    }
    public void SendRequest(byte[] data)
    {
        try
        {
           String Broadcastaddress = new String("255.255.255.255");
           int port = Define.PORT;
           DatagramSocket serverSocket = new DatagramSocket();
           serverSocket.setBroadcast(true);
           InetAddress IPAddress = InetAddress.getByName(Broadcastaddress);
           DatagramPacket sendPacket = new DatagramPacket(data,data.length,IPAddress,port);
           try
           {
                serverSocket.send(sendPacket);
           }
           catch(Exception ex1)
           {
               System.out.println(ex1.toString());
           }
        }
        catch(Exception ex)
        {
            
        }
    }
}
